package com.centanet.framework.network;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.android.volley.Request;
import com.centanet.framework.interfaces.HttpCallback;
import com.centanet.framework.interfaces.ILoggerTag;
import com.centanet.framework.model.CoreResponse;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 中原找房api
 * Created by guilin on 16/5/24.
 */
public abstract class CoreApi<T> extends AbsApi<CoreResponse<T>> {

    //请求参数K,V形式
    protected final ArrayMap<String, Object> mParams;

    public CoreApi(Class<?> aClass, HttpCallback<CoreResponse<T>> httpCallback) {
        super(aClass, httpCallback);
        mParams = new ArrayMap<>();
    }

    @Override
    public StringBuilder url() {
        String path = getPath();
        int method = getMethod();
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(getIp());
        if (TextUtils.isEmpty(path)) {
            Logger.t(ILoggerTag.FRAME_REQUEST).d("method: %s", new VolleyMethodToString(method).parse2Sting());
        } else {
            urlBuilder.append(path);
            Logger.t(ILoggerTag.FRAME_REQUEST).d("method: %s\npath : %s", new VolleyMethodToString(method).parse2Sting(), path);
        }
        setParams();
        switch (method) {
            case Request.Method.GET:
            case Request.Method.DELETE:
                if (!mParams.isEmpty()) {
                    Logger.t(ILoggerTag.FRAME_REQUEST).d("params : %s", new Gson().toJson(mParams));
                    urlBuilder.append("?");
                    final String paramsEncoding = getParamsEncoding();
                    try {
                        for (Map.Entry<String, Object> entry : mParams.entrySet()) {
                            urlBuilder.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
                            urlBuilder.append('=');
                            urlBuilder.append(URLEncoder.encode(String.valueOf(entry.getValue()), paramsEncoding));
                            urlBuilder.append('&');
                        }
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException("Encoding not supported: " + paramsEncoding, e);
                    }
                    urlBuilder.deleteCharAt(urlBuilder.length() - 1);
                }
                break;
            default:
                break;
        }
        Logger.t(ILoggerTag.FRAME_REQUEST).d("url : %s", urlBuilder.toString());
        return urlBuilder;
    }

    @Override
    public byte[] params() {
        if (getMethod() == Request.Method.POST) {
            Logger.t(ILoggerTag.FRAME_REQUEST).d("request params : %s", new Gson().toJson(mParams));
            return new ArrayMapEncode(mParams).encode(getParamsEncoding());
        } else {
            return new byte[0];
        }
    }

    /**
     * 设置请求参数
     */
    protected void putParams(String key, Object value) {
        mParams.put(key, value);
    }

    /**
     * 重置请求参数
     */
    public void resetParams() {
        mParams.clear();
    }
}
